package com.atguigu.ssyx.common.service;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service


public class RabbitService {
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * @param exchange   交换器名称
     * @param routingKey 路由键
     * @param message    消息内容
     * @return
     */
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return true;
    }

    public boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message, msg -> {
            // 设置延迟时间，单位为毫秒
            msg.getMessageProperties().setDelay(delayTime * 1000);
            return msg;
        });
        return true;
    }

}
